const service = require("../../../../service/port.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    unpayList: [],
    status: "7,8,9", //状态,1、未付款 2、已付款,未发货 3、已发货,未确认 4、交易成功5、交易关闭 6、已评价7、退货中 8、退货成功,9退货失败
    pageSize: 4,
    pageNum: 1,
    noData: false, // 是否有更多数据
    noDatas: false, //一条数据都没有   

    refundprogressShow: false,
    resons: "", //退款进度 退款原因
    images: [], //退款进度 图片
    statusText:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getList(callback) {
    let customerId = wx.getStorageSync("userInfo").id;
    service.getTbOrderList({
        tbOrder: {
          customerId: customerId, //客户id
          status: this.data.status,
        },
        queryRequest: {
          pageSize: this.data.pageSize,
          pageNum: this.data.pageNum,
        },
      },
      (res) => {
        //console.log(res)
        for (let i = 0; i < res.data.rows.length; i++) {
          const element = res.data.rows[i];
          if(element.timelyDelivery==1){
            if(element.status==7){
              this.setData({
                statusText:"退款中"
              })
            }else if(element.status==8){
              this.setData({
                statusText:"退款成功"
              })
            }else if(element.status==9){
              this.setData({
                statusText:"退款失败"
              })
            }
            
          }else if(element.timelyDelivery==2){
            if(element.postStatus==1){
              this.setData({
                statusText:"未配送"
              })
            }else if(element.postStatus==2){
              this.setData({
                statusText:"配送中"
              })
            }else if(element.postStatus==3){
              this.setData({
                statusText:"已配送"
              })
            }else{
              this.setData({
                statusText:"配送异常"
              })
            }

          }else{
            this.setData({
              statusText:"到店自取"
            })
          }
          
        }
        callback(res);
      }
    );
  },
  onLoad: function () {
    var self = this;
    // 订单列表
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(function (res) {
      if (res.data.total == 0) {
        self.setData({
          noDatas: true,
          noData:false
        });
      } else if (res.data.total > 0 && res.data.total < 4) {
        self.setData({
          unpayList: res.data.rows,
          noData: true,
        });
      } 
      self.setData({
        unpayList: res.data.rows,
      });
      wx.hideLoading();
    });
  },
  //跳转到订单详情
  productdetail: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index.id;
    wx.navigateTo({
      url: "/pages/order/orderDetail/index?id=" + index,
    });
  },
  unpayTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/unpayList/index",
    });
  },
  unreviceTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/unreviceList/index",
    });
  },
  completeTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/completeList/index",
    });
  },
  closerTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/closerList/index",
    });
  },
  refundTo: function (e) {
    wx.redirectTo({
      url: "/pages/mine/order/refundList/index",
    });
  },
  previewImg: function (e) {
    console.log(e);
    var imgUrls = e.currentTarget.dataset.item;
    //图片预览
    wx.previewImage({
      current: imgUrls, // 当前显示图片的http链接
      urls: this.data.images
    })
  },
  //退款进度 begin    
  progressShow: function (e) {
    console.log(e)
    let imagedata = e.currentTarget.dataset.index.images.split(",")
    this.setData({
      refundprogressShow: true,
      resons: e.currentTarget.dataset.index.returnReson,
      returnMoney: e.currentTarget.dataset.index.actualPay,
      images:imagedata,
      returnCreateTime: e.currentTarget.dataset.index.returnCreateTime,
      appStu: e.currentTarget.dataset.index.appStu,//审批状态 0待审批 1 同意 2拒绝
      appResult:e.currentTarget.dataset.index.appResult,
      returnModifyTime:e.currentTarget.dataset.index.returnModifyTime,

    })
  },
  //退款进度
  progressClose: function (e) {
    this.setData({
      refundprogressShow: false
    })
  },
  // 退款进度 end

 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求数据
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      if (res.data.rows.length > 0 && res.data.rows.length < 4) {
        self.setData({
          unpayList: res.data.rows,
          noData: true,
        });
      } else {
        self.setData({
          unpayList: res.data.rows,
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 判读数据是否加载完毕
    if (this.data.noData) return;
    // 停止下拉刷新
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    const unpayList = this.data.unpayList;
    let pageNum = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++pageNum,
    });
    const self = this;
    self.getList(function (res) {
      if (res.data.rows.length == 0) {
        self.setData({
          noData: true,
        });
      } else {
        res.data.rows.forEach((item) => {
          unpayList.push(item);
        });
        self.setData({
          unpayList: unpayList,
        });
      }
    });
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});